<?php

/*
*/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Stock extends CI_Controller {

	public $data;
	private $access_id = 200;
	private $limit = 15;
	private $error = '';
	private $controller_right;
	private $controller_right_name;
	private $bound_types;
	private $inbound_types;
	private $outbound_types;

	public function __construct()
	{
		parent::__construct();
		//载入必要类文件
		$this->load->model('Login_model');
		$this->load->model('Right_Control_model');
		$this->load->model('Order_model');
		$this->load->model('Order_Detail_model');
		$this->load->model('User_model');
		$this->load->model('Right_model');
		$this->load->model('Sub_goods_model');
		$this->load->model('Supplier_model');
		$this->load->model('Suppliers_Goods_model');
		$this->load->model('Stock_model');
		$this->load->model('Stock_Detail_model');
		$this->load->model('Inbound_Type_model');
		$this->load->model('Outbound_Type_model');
		//载入必要库文件
		$this->load->library('pagination');
		$this->load->library('Access_control');
		$this->load->library('Menu_control');
		$this->load->library('Forbid_resubmit');
		$this->load->library('Show_page');

		//载入配置文件 并 初始化控制器
		$this->data['site'] = $this->config->item('site');
		$this->data['TYPE'] = $this->config->item('TYPE');
		//var_dump($this->data['TYPE']);
		//$this->data['order_status'] = $this->config->item('order_status');
		//$this->data['purapp_status'] = $this->config->item('purapp_status');
		$this->data['title'] = '库存管理';
		$this->data['styles'] = array('error.css', 'stock.css', 'jQuery.jdatepicker.css');
		$this->data['scripts'] = array('master.js', 'stock.js', 'search_goods.js', 'jQuery.jdatepicker.js');

		//检查是否是登录用户
		if (!$this->Login_model->check(TRUE))
		{
			header("Location: ".$this->data['site']['root'].'/login');
		}

		//初始化参数
		$this->controller_right = 'stock';
		$this->controller_right_name = '库存管理';
		//设置站点基本参数
		$this->data['cur_user'] = $this->User_model->getUser($this->input->cookie('user_id'));
		$this->data['header']['username'] = $this->data['cur_user']['realname'];
		$this->data['site']['environment'] = $this->User_model->get_envrionment();
		$this->data['menus'] = $this->config->item('menus');
		$this->data['menus'] = $this->menu_control->main($this->data['menus'], 1);
		$this->data['sub_menus'] = $this->_menus();
		$this->data['sub_menu_index'] = '-1';
	}

	private function _menus()
	{
		return array(
			array(
				'index' =>	1,
				'url'	=>	$this->data['site']['root'] . 'stock/lst/',
				'name'	=>	'所有库存单',
				'right' => 'stock.lst'
			),
			array(
				'index'	=>	0,
			),
			array(
				'index' =>	21,
				'url'	=>	$this->data['site']['root'] . 'stock/create/inbound/order',
				'name'	=>	'进货入库',
				'right' => 'stock._create_inbound_order'
			),
			array(
				'index' =>	22,
				'url'	=>	$this->data['site']['root'] . 'stock/create/inbound/return',
				'name'	=>	'退货入库',
				'right' => 'stock._create_inbound_return'
			),
			array(
				'index' =>	24,
				'url'	=>	$this->data['site']['root'] . 'stock/create/inbound/exchange',
				'name'	=>	'换货入库',
				'right' => 'stock._create_inbound_exchange'
			),
			array(
				'index' =>	23,
				'url'	=>	$this->data['site']['root'] . 'stock/create/inbound/other',
				'name'	=>	'其他入库',
				'right' => 'stock._create_inbound_other'
			),
			array(
				'index'	=>	0,
			),
			array(
				'index' =>	31,
				'url'	=>	$this->data['site']['root'] . 'stock/create/outbound/sale',
				'name'	=>	'销售出库',
				'right' => 'stock._create_inbound_sale'
			),
			array(
				'index' =>	35,
				'url'	=>	$this->data['site']['root'] . 'stock/create/outbound/exchange',
				'name'	=>	'换货出库',
				'right' => 'stock._create_inbound_exchange'
			),
			array(
				'index' =>	32,
				'url'	=>	$this->data['site']['root'] . 'stock/create/outbound/return',
				'name'	=>	'退货出库-退厂',
				'right' => 'stock._create_inbound_return'
			),
			array(
				'index' =>	33,
				'url'	=>	$this->data['site']['root'] . 'stock/create/outbound/loss',
				'name'	=>	'损耗出库',
				'right' => 'stock._create_inbound_order'
			),
			array(
				'index' =>	34,
				'url'	=>	$this->data['site']['root'] . 'stock/create/outbound/other',
				'name'	=>	'其他出库',
				'right' => 'stock._create_inbound_other'
			),
			array(
				'index'	=>	0,
			),
			array(
				'index' =>	41,
				'url'	=>	$this->data['site']['root'] . 'stock/create/transfer',
				'name'	=>	'调仓',
				'right' => 'stock._create_inbound_order'
			)
		);
	}

<<<<<<< HEAD
	private function _set_menu($index, $title)
	{
		$this->data['sub_menu_index'] = $index;
		$this->data['main']['title'] = $title;
	}

=======
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
	public function index()
	{
		$this->lst();
	}

	public function create($bound_type='', $type='')
	{

		//初始化模型
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();

		//是否提交表单
		$error = false;
		if ($this->input->post('submit'))
		{
			$this->db->trans_begin();
			$this->Stock_model->create();
			if (empty($this->Stock_model->error))
			{
				//$this->db->trans_rollback();
				$this->db->trans_commit();
				$this->data['ajax']['request'] = 'DONE';
				$this->data['ajax']['data'] = '添加成功 :)';
			}
			else
			{
				$this->db->trans_rollback();
				$this->data['ajax']['request'] = 'ERROR';
				$this->data['ajax']['data'] = $this->Stock_model->error;
			}
			$this->load->view('ajax_json', $this->data);
			return;
		}

		//判断创建库存当类型类型
		if (!empty($bound_type))
		{
			//判断是否是入库
			if ('inbound' == $bound_type)
			{
				$this->data['form']['stock_type'] = 'inbound';
				switch ($type) {
					case 'order':
					case 'return':
					case 'exchange':
					case 'other':
						break;
					default:
						$this->data['error'] = '未知的入库类型';
						$this->load->view_('error_500_view', $this->data);
						return FALSE;
				}
			}
			//判断是否是出库
			else if ('outbound' == $bound_type)
			{
				$this->data['form']['stock_type'] = 'outbound';
				switch ($type) {
					case 'sale':
					case 'return':
					case 'exchange':
					case 'loss':
					case 'other':
						break;
					default:
						$this->data['error'] = '未知的入库类型';
						$this->load->view_('error_500_view', $this->data);
						return FALSE;
				}
			}
			//判断是否是调货
			else if ('transfer' == $bound_type)
			{
				$this->data['form']['stock_type'] = 'transfer';
			}
			else
			{
				$this->data['error'] = '未知的创建类型。';
				$this->load->view_('error_500_view', $this->data);
				return FALSE;
			}
			
			if ($this->_create_bound_type($bound_type, $type))
			{
				$this->data['form']['stock_bound_type'] = $type;
				$this->data['form']['bound_types'] = $this->bound_types;
			}
			else
			{
				$this->data['error'] = 'SYS_ERR';
				$this->load->view_('error_500_view', $this->data);
				return FALSE;
			}

		}
		else
		{
			$this->data['error'] = '未知的创建类型。';
			$this->load->view_('error_500_view', $this->data);
			return FALSE;
		}

		$this->load->view_('stock_create_view', $this->data);
	}

	private function _create_bound_type($bound_type, $type)
	{
		if ('transfer' == $bound_type)
		{
			$func = '_create_transfer';
		}
		else
		{
			$func = '_create_' . $bound_type . '_' . $type;
		}
		if (method_exists($this, $func))
		{
			$this->$func();
			if (!empty($this->bound_types))
			{
				return TRUE;
			}
		}
		return FALSE;
	}

	private function _create_inbound_order()
	{
		$this->data['search_type'] = 'S_P';
		$this->data['sub_title'] = '进货入库';
		$this->data['sub_menu_index'] = 21;
		$this->Inbound_Type_model->get_by_flg('order_flg');
		$this->bound_types = $this->Inbound_Type_model->inbound_types();
	}

	private function _create_inbound_return()
	{
		$this->data['search_type'] = 'N';
		$this->data['sub_title'] = '退货入库';
		$this->data['sub_menu_index'] = 22;
		$this->Inbound_Type_model->get_by_flg('return_flg');
		$this->bound_types = $this->Inbound_Type_model->inbound_types();
	}

	private function _create_inbound_exchange()
	{
		$this->data['search_type'] = 'N';
		$this->data['sub_title'] = '换货入库';
		$this->data['sub_menu_index'] = 24;
		$this->Inbound_Type_model->get_by_flg('exchange_flg');
		$this->bound_types = $this->Inbound_Type_model->inbound_types();
	}

	private function _create_inbound_other()
	{
		$this->data['search_type'] = 'N';
		$this->data['sub_title'] = '其他入库';
		$this->data['sub_menu_index'] = 23;
		$this->Inbound_Type_model->get_by_flg('other_flg');
		$this->bound_types = $this->Inbound_Type_model->inbound_types();
	}

	private function _create_outbound_sale()
	{
		$this->data['search_type'] = 'N';
		$this->data['sub_title'] = '销售出库';
		$this->data['sub_menu_index'] = 31;
		$this->Outbound_Type_model->get_by_flg('sale_flg');
		$this->bound_types = $this->Outbound_Type_model->outbound_types();
	}

	private function _create_outbound_return()
	{
		$this->data['search_type'] = 'S';
		$this->data['sub_title'] = '退货出库-退厂';
		$this->data['sub_menu_index'] = 32;
		$this->Outbound_Type_model->get_by_flg('return_flg');
		$this->bound_types = $this->Outbound_Type_model->outbound_types();
	}

	private function _create_outbound_exchange()
	{
		$this->data['search_type'] = 'N';
		$this->data['sub_title'] = '换货出库';
		$this->data['sub_menu_index'] = 35;
		$this->Outbound_Type_model->get_by_flg('exchange_flg');
		$this->bound_types = $this->Outbound_Type_model->outbound_types();
	}

	private function _create_outbound_loss()
	{
		$this->data['search_type'] = 'N';
		$this->data['sub_title'] = '损耗出库';
		$this->data['sub_menu_index'] = 33;
		$this->Outbound_Type_model->get_by_flg('loss_flg');
		$this->bound_types = $this->Outbound_Type_model->outbound_types();
	}

	private function _create_outbound_other()
	{
		$this->data['search_type'] = 'N';
		$this->data['sub_title'] = '其他出库';
		$this->data['sub_menu_index'] = 34;
		$this->Outbound_Type_model->get_by_flg('other_flg');
		$this->bound_types = $this->Outbound_Type_model->outbound_types();
	}

	private function _create_transfer()
	{
		$this->data['search_type'] = 'transfer';
		$this->data['type'] = 'transfer';
		$this->data['sub_title'] = '调货';
		$this->data['sub_menu_index'] = 41;
		$this->bound_types = array(
			array('id'=>0, 'name'=>'调仓')
			);
	}

<<<<<<< HEAD
	//统计数据
	public function statistics()
	{
		
	}

=======
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
	public function lst($page=1)
	{
		//分配权限
		$this->controller_right = strtolower(__CLASS__ . '.' . __FUNCTION__);

		//载入必要模型
		$this->load->model('Stock_model');		//库存模型

		//载入帮助函数
		$this->load->helper('check_date_helper');

		//初始化模型
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();

		//获得所有库存类型
		$this->Inbound_Type_model->get_all();
		$this->Outbound_Type_model->get_all();
		$this->data['inbound_types'] = $this->Inbound_Type_model->inbound_types('ID_NAME');
		$this->data['outbound_types'] = $this->Outbound_Type_model->outbound_types('ID_NAME');

		$data = '';
		$this->data['sub_menu_index'] = 1;
		$start_date_mktime 	= FALSE;
		$end_date_mktime	= FALSE;
		$description		= FALSE;
		$stock_type 		= FALSE;
		$bound_type_ids 	= FALSE;
		$this->data['search'] = FALSE;

		//判断页码
		if (!preg_match("/^\d*$/", $page))
		{
			$page = 1;
		}

		$base_url = $this->data['site']['root'] . 'stock/lst/';
		$suffix = '';
		$uri_segment = 3;

		//处理用户输入数据
		$start_date = trim($this->input->get('start_date'));		//开始日期
		$end_date = trim($this->input->get('end_date'));			//结束日期
		$description = trim($this->input->get('description'));	//备注
		$stock_type = trim($this->input->get('stock_type'));		//库存类型
		$bound_type_flg = trim($this->input->get('bound_type_flg'));	//系统库存类型标识
		$bound_type_id = trim($this->input->get('bound_type_id'));	//指定库存类型ID

		//起始日期
		if ($start_date && $start_date != '')
		{
			//添加链接后缀
			$suffix .= 'start_date='.$start_date.'&';
			if ($start_date_mktime = check_date($start_date, TRUE))
			{
				$this->data['search']['start_date'] = &$start_date;
			}
		}
		//结束日期
		if ($end_date && $end_date != '')
		{
			//添加链接后缀
			$suffix .= 'end_date='.$end_date.'&';
			if ($end_date_mktime = check_date($end_date, TRUE))
			{
				$this->data['search']['end_date'] = &$end_date;
			}
		}
		//比较日期顺序
		if ($start_date_mktime && $end_date_mktime)
		{
			//判断后重新处理前后顺序
			if ($start_date_mktime > $end_date_mktime)
			{
				$temp = $start_date;
				$start_date = $end_date;
				$end_date = $temp;
				unset($temp);
			}
		}
		//备注
		if ($description != '')
		{
			$suffix .= 'description='.urlencode($description).'&';
			$this->data['search']['description'] = &$description;
		}
		//出库、入库、调仓
		if ($stock_type != '')
		{
			$suffix .= 'stock_type='.$stock_type.'&';
			$this->data['search']['stock_type'] = &$stock_type;
			//校验库存类型
			switch ($stock_type) {
				case 'all':
					$stock_type = FALSE;
					break;

				case 'inbound':
					//校验系统库存类型
					$this->data['form']['bound_type_flg'] = $this->Inbound_Type_model->inbound_flgs();
					$this->data['search']['bound_type_flg'] = FALSE;
					if ('' != $bound_type_flg)
					{
						if ('all' != $bound_type_flg)
						{
							if ($bound_type_ids = $this->Inbound_Type_model->valid_flg($bound_type_flg, TRUE))
							{
								$suffix .= 'bound_type_flg='.$bound_type_flg.'&';
								$this->data['search']['bound_type_flg'] = &$bound_type_flg;
								$this->Inbound_Type_model->get_by_flg($bound_type_flg);
								$this->data['form']['bound_type_id'] = $this->Inbound_Type_model->inbound_types('ID_NAME');
								$this->data['search']['bound_type_id'] = FALSE;

								//验证自定义库存类型
								if ('' != $bound_type_id)
								{
									if ('all' != $bound_type_id)
									{
										if ($this->Inbound_Type_model->get_one($bound_type_id, TRUE))
										{
											$suffix .= 'bound_type_id='.$bound_type_id.'&';
											$this->data['search']['bound_type_id'] = &$bound_type_id;
											$bound_type_ids = array($bound_type_id);
										}
									}
								}
							}
						}
					}
					break;

				case 'outbound':
					$this->data['form']['bound_type_flg'] = $this->Outbound_Type_model->outbound_flgs();
					$this->data['search']['bound_type_flg'] = FALSE;
					//校验系统库存类型
					if ('' != $bound_type_flg)
					{
						if ('all' != $bound_type_flg)
						{
							if ($bound_type_ids = $this->Outbound_Type_model->valid_flg($bound_type_flg, TRUE))
							{
								$suffix .= 'bound_type_flg='.$bound_type_flg.'&';
								$this->data['search']['bound_type_flg'] = &$bound_type_flg;
								$this->Outbound_Type_model->get_by_flg($bound_type_flg);
								$this->data['form']['bound_type_id'] = $this->Outbound_Type_model->outbound_types('ID_NAME');
								$this->data['search']['bound_type_id'] = FALSE;

								//验证自定义库存类型
								if ('' != $bound_type_id)
								{
									if ('all' != $bound_type_id)
									{
										if ($this->Outbound_Type_model->get_one($bound_type_id, TRUE))
										{
											$suffix .= 'bound_type_id='.$bound_type_id.'&';
											$this->data['search']['bound_type_id'] = &$bound_type_id;
											$bound_type_ids = array($bound_type_id);
										}
									}
								}
							}
						}
					}
					break;

				case 'transfer':
					break;
				default:
					$stock_type = FALSE;
					$this->error = 'FORM_ERR|STOCK_TYPE';
			}
		}

		if ($suffix != '')
		{
			$suffix = '?'.$suffix;
		}

<<<<<<< HEAD
		//获得库存列表
=======
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
		$data = $this->Stock_model->get_list($this->limit, ($page-1)*$this->limit, 'timeline', $start_date_mktime, $end_date_mktime, $description, $stock_type, $bound_type_ids);
		if ($this->data['error'] = $this->Stock_model->error)
		{
			$this->load->view_('error_500_view', $this->data);
			return;
		}

		$this->data['page_show'] = $this->show_page->_pages($data['count'], $base_url, $suffix, $page, $this->limit, $uri_segment);
		$this->data['page_show'] = $this->data['page_show']['display'];
		$this->data['count'] = $data['count'];
		$this->data['page_count'] = ceil($data['count'] / $this->limit);
		$this->data['page'] = $page;
		$this->data['stocks'] = $data['result'];
		$this->load->view_('stock_list_view', $this->data);
	}

	public function detail($stock_id)
	{

		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();

		//获得所有库存类型
		$this->Inbound_Type_model->get_all();
		$this->Outbound_Type_model->get_all();
		$this->data['inbound_types'] = $this->Inbound_Type_model->inbound_types('ID_NAME');
		$this->data['outbound_types'] = $this->Outbound_Type_model->outbound_types('ID_NAME');

		//分配权限
		$this->controller_right = strtolower(__CLASS__ . '.' . __FUNCTION__);

		$this->data['sub_menus'] = $this->menu_control->add_sub($this->data['sub_menus'], array('index'=>101,'access'=>0,'url'=>'javascript:void(0)','name'=>'详情'), TRUE);
		$this->data['sub_menu_index'] = 101;
		$this->data['stock'] = $this->Stock_model->get_one($stock_id, 'STOCK_ID');
		if ($this->data['error'] = $this->Stock_model->error)
		{
			$this->load->view_('stock_detail_view', $this->data);
		}
		$this->load->view_('stock_detail_view', $this->data);
	}

	//撤销一条库存单
	//Ajax请求
	public function delete()
	{

		//分配权限
		$this->controller_right = strtolower(__CLASS__ . '.' . __FUNCTION__);

		$this->db->trans_begin();
		if ($this->Stock_model->delete())
		{
			$this->db->trans_commit();
			$this->data['ajax']['request'] = 'DONE';
			$this->data['ajax']['data'] = '删除成功！'; 
		}
		else
		{
			$this->db->trans_rollback();
			$this->data['ajax']['request'] = 'FAILED';
			$this->data['ajax']['data'] = '删除失败，请刷新后重试或联系管理员！' . $this->Stock_model->error;
		}
		$this->load->view('ajax_json', $this->data);
	}

	//Ajax 搜索商品列表
	public function search()
	{
		$text = $this->input->post('q');
		$this->data['ajax']['request'] = 'SEARCH_DONE';
		$this->data['ajax']['data'] = $this->Sub_goods_model->search('ss', $text, TRUE);
		$this->load->view('ajax_json', $this->data);
	}

	public function ajax_get_suppliers()
	{
		if ($sub_goods_id = $this->input->post('sub_goods_id'))
		{
			$suppliers = $this->Suppliers_Goods_model->get_suppliers($sub_goods_id, true);
			$this->data['ajax']['request'] = 'GET_DONE';
			$this->data['ajax']['data'] = $suppliers;
			$this->load->view('ajax_json', $this->data);
		}
		else
		{
			$this->data['ajax']['request'] = 'ERROR';
			$this->data['ajax']['data'] = '无效的子货品';
			$this->load->view('ajax_json', $this->data);
		}

	}

}